#!/bin/sh

# Purpose: 	set up the runtime environment and run GWC (adapted from GIMP)

echo "Setting up environment..."

if test "x$GTK_DEBUG_LAUNCHER" != x; then
    set -x
fi

if test "x$GTK_DEBUG_GDB" != x; then
    EXEC="gdb --args"
else
    EXEC=exec
fi

name=`basename "$0"`
tmp="$0"
tmp=`dirname "$tmp"`
tmp=`dirname "$tmp"`
bundle=`dirname "$tmp"`
bundle_contents="$bundle"/Contents
bundle_res="$bundle_contents"/Resources
bundle_lib="$bundle_res"/lib
bundle_bin="$bundle_res"/bin
bundle_data="$bundle_res"/share
bundle_etc="$bundle_res"/etc

export DYLD_LIBRARY_PATH="$bundle_lib"
#it could be useful to set some XDG environment variables - by default gwc uses ~/.cache and ~/.config
#export XDG_CONFIG_DIRS="$bundle_etc"/xdg
#export XDG_DATA_DIRS="$bundle_data"

#we might need some of these for themes or whatever
export GTK_DATA_PREFIX="$bundle_res"
export GTK_EXE_PREFIX="$bundle_res"
export GTK_PATH="$bundle_res"

#we need XDG_CACHE_HOME to prevent terrible behaviour when running from a .dmg, caused by
#falling back to the current working directory, which is read-only in that case.
export XDG_CACHE_HOME="$HOME/Library/Caches"
#it is tidier if we use this (unless there are users on osx who like to have stuff in ~/.config?)
export XDG_CONFIG_HOME="$HOME/Library/gtk-wave-cleaner/"
#not sure if this is needed
export XDG_DATA_HOME="$HOME/Library/gtk-wave-cleaner/"

# Set up PATH variable
export PATH="$bundle_contents/MacOS:$PATH"

# Set up generic configuration
export GTK2_RC_FILES="$bundle_etc/gtk-2.0/gtkrc"
export GDK_PIXBUF_MODULE_FILE="$bundle_lib/gdk-pixbuf-2.0/loaders.cache"
# Alister: this doesn't actually seem to do anything:
# export GDK_PIXBUF_MODULEDIR="$bundle_lib/gdk-pixbuf-2.0/loaders"

# Extra arguments can be added in environment.sh.
EXTRA_ARGS=
if test -f "$bundle_res/environment.sh"; then
 source "$bundle_res/environment.sh"
fi

# Strip out the argument added by the OS.
if /bin/expr "x$1" : '^x-psn_' > /dev/null; then
 shift 1
fi

# change to the user's home directory so that anything written to the current working directory ends up there.
cd $HOME

echo "Launching Gtk Wave Cleaner..."
$EXEC "$bundle_contents/MacOS/gtk-wave-cleaner-bin" "$@" $EXTRA_ARGS
